home *** CD-ROM | disk | FTP | other *** search
/ Super PC 31 / Super PC 31 (Shareware).iso / spc / inter / winpm223 / forms / cryptxor / encrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-23  |  4.2 KB  |  163 lines

  1. //
  2. //  ENCRYPT.C
  3. //  Runtime Loadable Encryptor Extension for Pegasus Mail for Windows.
  4. //  Copyright (c) 1995, David Harris, All Rights Reserved.
  5. //
  6. //  Encryptor module: this module implements the actual Encryptor
  7. //  Interface Routines called by WinPMail at runtime. For more
  8. //  information on the actual form and function of each call, consult
  9. //  the file CRYPTIF.TXT in the FORMS subdirectory of your WINPMAIL
  10. //  installation directory.
  11. //
  12. //  The author grants explicit permission for this source code to be
  13. //  used or modified as required, subject only to the conditions that
  14. //  the copyright notices above are preserved and that by using this
  15. //  code you agree that the code is provided without warranty of any
  16. //  kind, either explicit or implied, and you use it at your own
  17. //  risk.
  18. //
  19. //  *** WARNING!! The encryption method this module uses barely even
  20. //  *** warrants the title "encryptor"; it is a very simple algorithm
  21. //  *** that should be quite easy to break for anyone wishing to invest
  22. //  *** some time in standard cryptanalytical techniques.
  23. //  ***
  24. //  *** UNDER NO CIRCUMSTANCES SHOULD YOU REGARD THIS AS A WORKING
  25. //  *** ENCRYPTOR OFFERING ANY KIND OF SECURITY. IT IS INTENDED PURELY
  26. //  *** AS TUTORIAL CODE SHOWING IMPLEMENTATIONAL DETAIL.
  27. //
  28. //  This sample implementation does not support digital signatures or
  29. //  public keys and thus does not implement the following calls:
  30. //
  31. //     int FAR PASCAL SIGN_FILE (char *sourcefile, char *destfile,
  32. //        char *recipient, char *key, int encrypt_as_well);
  33. //     int FAR PASCAL VERIFY_SIGNATURE (char *sourcefile);
  34. //     int FAR PASCAL (*KEY_MANAGEMENT) (char *sourcefile);
  35.  
  36. #include <windows.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <io.h>
  40.  
  41. #pragma warn -par       //  Not worried about unused parameters
  42.  
  43.  
  44. int FAR PASCAL _export ENCRYPT_FILE (char *sourcefile,
  45.    char *destfile, char *recipient, char *key, int textfile)
  46.    {
  47.    FILE *infil, *outfil;
  48.    int c, i, result;
  49.    char *s;
  50.  
  51.    if ((infil = fopen (sourcefile, "rb")) == NULL) return 0;
  52.    if ((outfil = fopen (destfile, "wb")) == NULL)
  53.       {
  54.       fclose (infil);
  55.       return 0;
  56.       }
  57.  
  58.    s = key;
  59.    i = 0;
  60.    result = 1;
  61.    fputc ((char) 127, outfil);
  62.    fputc ('\n', outfil);
  63.    while ((c = fgetc (infil)) != EOF)
  64.       {
  65.       c ^= ((*s) + i);
  66.       ++ s;
  67.       if (*s == '\0')
  68.          {
  69.          s = key;
  70.          ++ i;
  71.          if (i >= 20) i = 0;
  72.          }
  73.  
  74.       if ((c < ' ') || (c > 127)) result = 2;
  75.       fputc (c, outfil);
  76.       }
  77.    fputc ((char) 127, outfil);
  78.    fputc ('\n', outfil);
  79.    fclose (infil);
  80.    fclose (outfil);
  81.    return result;
  82.    }
  83.  
  84.  
  85. int FAR PASCAL _export DECRYPT_FILE (char *sourcefile, char *destfile, char *key)
  86.    {
  87.    FILE *infil, *outfil;
  88.    int c, i, result, working, trigger;
  89.    char *s;
  90.  
  91.    if ((infil = fopen (sourcefile, "rb")) == NULL) return 0;
  92.    if ((outfil = fopen (destfile, "wb")) == NULL)
  93.       {
  94.       fclose (infil);
  95.       return 0;
  96.       }
  97.  
  98.    s = key;
  99.    i = working = 0;
  100.    trigger = 0;
  101.    result = 1;
  102.    while ((c = fgetc (infil)) != EOF)
  103.       {
  104.       if (trigger == 1)
  105.          {
  106.          trigger = 0;
  107.          if (c == '\n')
  108.             {
  109.             working ^= 1;
  110.             continue;
  111.             }
  112.          else
  113.             {
  114.             ungetc (c, infil);
  115.             c = 127;
  116.             }
  117.          }
  118.       else if (c == 127)
  119.          {
  120.          trigger = 1;
  121.          continue;
  122.          }
  123.  
  124.       if (working)
  125.          {
  126.          c ^= ((*s) + i);
  127.          ++ s;
  128.          if (*s == '\0')
  129.             {
  130.             s = key;
  131.             ++ i;
  132.             if (i >= 20) i = 0;
  133.             }
  134.          }
  135.  
  136.       if ((c < ' ') || (c > 127)) result = 2;
  137.       fputc (c, outfil);
  138.       }
  139.  
  140.    fclose (infil);
  141.    fclose (outfil);
  142.    return result;
  143.    }
  144.  
  145.  
  146. int FAR PASCAL _export DESTROY_FILE (char *sourcefile)
  147.    {
  148.    FILE *fil;
  149.    long len;
  150.  
  151.    if ((fil = fopen (sourcefile, "r+b")) == NULL) return 0;
  152.    len = filelength (fileno (fil));
  153.    while (len)
  154.       {
  155.       fputc ('\0', fil);
  156.       -- len;
  157.       }
  158.    fclose (fil);
  159.    remove (sourcefile);
  160.    return 1;
  161.    }
  162.  
  163.